// GTY


#include "AbilitySystem/Abilities/AuraProjectileSpell.h"

#include "AbilitySystemBlueprintLibrary.h"
#include "AbilitySystemComponent.h"
#include "Actor/AuraProjectile.h"
#include "Interaction/CombatInterface.h"
#include "auragame/Public/AuraGameplayTags.h"



void UAuraProjectileSpell::ActivateAbility(
	const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo,
	const FGameplayAbilityActivationInfo ActivationInfo, const FGameplayEventData* TriggerEventData
)
{
	Super::ActivateAbility(Handle, ActorInfo, ActivationInfo, TriggerEventData);


	
}

void UAuraProjectileSpell::SpawnProjectile(const FVector& ProjectileTargetLocation, const FGameplayTag& SocketTag, bool bOverridePitch, float PitchOverride )
{
	
	const bool bIsServer = GetAvatarActorFromActorInfo()->HasAuthority();
	if (!bIsServer) return;

	ICombatInterface* CombatInterface = Cast<ICombatInterface>(GetAvatarActorFromActorInfo());//获取角色
	if (CombatInterface)
	{
		const FVector SocketLocation = ICombatInterface::Execute_GetCombatSocketLocation(GetAvatarActorFromActorInfo(),SocketTag);

		FRotator SocketRotation = (ProjectileTargetLocation - SocketLocation).Rotation();
		//SocketRotation.Pitch= 0.f;//角度归零，使其平射
		
		if (bOverridePitch)
		{
			SocketRotation.Pitch = PitchOverride;
		}
		
		FTransform SpawnTransform;//生成的位置
		SpawnTransform.SetLocation(SocketLocation);
		SpawnTransform.SetRotation(SocketRotation.Quaternion());
		
		AAuraProjectile* Projectile = GetWorld()->SpawnActorDeferred<AAuraProjectile>//延迟生成
		(
			ProjectileClass,//指定生成的演员类。
			SpawnTransform,//指定生成位置和旋转。
			GetOwningActorFromActorInfo(),//指定生成的演员的拥有者。
			Cast<APawn>(GetOwningActorFromActorInfo()),//指定生成的演员的归属者。
			ESpawnActorCollisionHandlingMethod::AlwaysSpawn//指定生成的演员的碰撞处理方式。始终生成。
		);

	
		Projectile->DamageEffectParams = MakeDamageEffectParamsFromClassDefaults();

		
		Projectile->FinishSpawning(SpawnTransform);//完成生成


		
		/*以被迭代
		//赋予游戏效果规范
		//创建一个GE的实例，并设置给投射物
		const UAbilitySystemComponent* SourceASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetAvatarActorFromActorInfo());
		FGameplayEffectContextHandle EffectContextHandle = SourceASC->MakeEffectContext();
			EffectContextHandle.SetAbility(this); //设置技能
			EffectContextHandle.AddSourceObject(Projectile); //设置GE的源
			//添加Actor列表
			TArray<TWeakObjectPtr<AActor>> Actors;
			Actors.Add(Projectile);
			EffectContextHandle.AddActors(Actors);
			//添加命中结果 ?
			FHitResult HitResult;
			HitResult.Location = ProjectileTargetLocation;
			EffectContextHandle.AddHitResult(HitResult);
			//添加技能触发位置
			EffectContextHandle.AddOrigin(ProjectileTargetLocation);

		
		
		const FGameplayEffectSpecHandle SpecHandle = SourceASC->MakeOutgoingSpec(DamageEffectClass, GetAbilityLevel(),EffectContextHandle);

		const FAuraGameplayTags GameplayTags = FAuraGameplayTags::Get(); //获取标签单例

		const float ScaledDamage = Damage.GetValueAtLevel(GetAbilityLevel()); //根据等级获取技能伤害
		UAbilitySystemBlueprintLibrary::AssignTagSetByCallerMagnitude(SpecHandle, DamageType, ScaledDamage);

		Projectile->DamageEffectSpecHandle = SpecHandle;

		Projectile->FinishSpawning(SpawnTransform);//完成生成
		*/
	}
}
